package com.hsmpay.mobile.dao.organization.impl;

import com.hsmpay.common.dao.Impl.BaseDaoSupport;
import com.hsmpay.common.util.OrderTypeUtil;
import com.hsmpay.mobile.dao.organization.OrgPreDepositDao;
import com.hsmpay.mobile.dao.organization.OrgPreDepositLogDao;
import com.hsmpay.mobile.pojo.organization.OrgPreDeposit;
import com.hsmpay.mobile.pojo.organization.OrgPreDepositLog;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * 登录消息 Dao接口
 *
 * @author 颜铃璋
 *         建立时间：2014-3-21
 */
@Repository("orgPreDepositDao")
public class OrgPreDepositDaoImpl extends BaseDaoSupport<OrgPreDeposit, Long> implements OrgPreDepositDao<OrgPreDeposit, Long> {
    //	@Resource(name="orgTerminalDao")
//	private OrgTerminalDao<OrgTerminal,Long> orgTerminalDao;
    @Resource(name = "orgPreDepositLogDao")
    private OrgPreDepositLogDao<OrgPreDepositLog, Long> orgPreDepositLogDao;

    /**
     * 查询 预贷款
     *
     * @param terminalTypeId
     * @param roleId
     * @return
     * @throws SQLException
     */
    public double getPreDepositByOrgId(Long terminalTypeId, Long roleId) throws SQLException {
        Long organizationId = 1L;
        //Long organizationId = orgTerminalDao.getOrganizationId(terminalTypeId,roleId);

        OrgPreDeposit orgp = new OrgPreDeposit();
        orgp.setOrganizationId(organizationId);
        orgp = searchEntity(orgp);//(OrgPreDeposit)getSqlMapClientTemplate().queryForObject("orgPreDepositSqlMap.searchEntity", orgp);
        return null == orgp ? 0.00 : orgp.getPreDeposit();
    }

    /**
     * 添加 或 减少 预存款
     *
     * @param terminalTypeId
     * @param roleId
     * @param preDeposit     传入的是正数
     * @return
     * @throws SQLException
     */
    public int updateCashMoneyPreDeposit(Long terminalTypeId, Long roleId, Double preDeposit, String orderNum, Long userId) throws SQLException {
        Long organizationId = 1L;
        //Long organizationId = orgTerminalDao.getOrganizationId(terminalTypeId,roleId);
        //double tempPreDeposit = getPreDepositByOrgId(terminalTypeId,roleId);//现在数据库 金额

        OrgPreDeposit orgp = new OrgPreDeposit();
        orgp.setOrganizationId(organizationId);
        orgp.setPreDeposit(0 - preDeposit);
        int flag = getSqlMapClientTemplate().update("orgPreDepositSqlMap.updateAddPreDeposit", orgp);

        if (flag > 0) {
            //添加日志
            OrgPreDepositLog orgpLog = new OrgPreDepositLog();
            orgpLog.setOrganizationId(organizationId);
            orgpLog.setOrderNum(orderNum);
            orgpLog.setType(1);
            orgpLog.setCategory(OrderTypeUtil.CASH_MONEY_STR);
            orgpLog.setUserId(userId);
            //orgpLog.setBalance(tempPreDeposit);
            orgpLog.setIncExp(0);//负
            orgpLog.setTransMoney(preDeposit);
            orgPreDepositLogDao.insertEntity(orgpLog);
        }
        return flag;
    }
}
